/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Modelo.Cine;
import Modelo.GestorTicketCine;
import Modelo.Pelicula;
import Modelo.Provincia;
import Modelo.Sala;
import Modelo.Sesion;
import Modelo.TicketCine;
import Modelo.Usuario;
import java.util.ArrayList;

/**
 *
 * @author Bepitic
 */
public class InsertarPelicula extends javax.swing.JFrame {

    /**
     * Creates new form InsertarPelicula
     */
    public ArrayList<Provincia> provincia;
    private Pelicula peli;
    private Usuario usuario;
    public TicketCine tk;
    private GestorTicketCine gtc;
    
    public InsertarPelicula(GestorTicketCine gtk, Pelicula peli_a_insertar) {
        peli = peli_a_insertar;
        provincia = gtk.getTicketCine().getProvincias();
        usuario = new Usuario(gtk.getTicketCine());
        usuario.iniciarConsultar(provincia);
        gtc = gtk;
        initComponents();
        
        provincias.addItem("Default");
        for (Object pr : provincia) {
            provincias.addItem(pr.toString());
        }
        
        pack();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        provincias = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jBinsertarPeli = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        cine = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        provincias.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                provinciasActionPerformed(evt);
            }
        });

        jLabel5.setText("Cine:");

        jLabel4.setText("Provincia");

        jBinsertarPeli.setText("Insertar Pelicula");
        jBinsertarPeli.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBinsertarPeliActionPerformed(evt);
            }
        });

        jLabel1.setText("Introduce los datos del cine donde quieres insertar la pelicula");

        cine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cineActionPerformed(evt);
            }
        });

        jButton1.setText("Cancelar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(30, 30, 30)
                .add(jButton1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jBinsertarPeli)
                .add(63, 63, 63))
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(20, 20, 20)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel4)
                            .add(jLabel5))
                        .add(22, 22, 22)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, cine, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.LEADING, provincias, 0, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(jLabel1)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(12, 12, 12)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(provincias, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(cine, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 23, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jBinsertarPeli)
                    .add(jButton1))
                .add(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void provinciasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_provinciasActionPerformed
        ArrayList<Modelo.Cine> cines_loc = new ArrayList<>();
        String provincia1 = (String) provincias.getSelectedItem();
        cines_loc = usuario.buscarCinesPorProvincia(provincia1);
        cine.removeAllItems();
        for( Object c : cines_loc ){
            cine.addItem(c.toString());
        }

    }//GEN-LAST:event_provinciasActionPerformed

    private void jBinsertarPeliActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBinsertarPeliActionPerformed
       System.out.println("Hasta aqui todo bien");
       
        ArrayList<Cine> cines_loc = new ArrayList<>();
        String cine1 = (String) cine.getSelectedItem();
        String provincia1 = (String) provincias.getSelectedItem();
        int fecha = peli.getFechaestreno();
        String nombrepeli = peli.getTitulo();
          
        Cine cineelegido = new Cine(null, null, null, null, 0);
        cines_loc = usuario.buscarCinesPorProvincia(provincia1);
        for (Cine p : cines_loc) {
            if (cine1.equals(p.getCine())) {
                cineelegido = p;
            }
        }    
        if(cineelegido.getCine() != null){
         cineelegido.addPelicula(peli);
         tk =  usuario.getTicketCine();
         tk.setProvincias(provincia);
         usuario.setTicketCine(tk);
         usuario.setGestorTicketCine(gtc);
         gtc.realizarBackup();
        }
        else{
            System.out.println("NO se ha encontradu el cine seleccionado");
            Vista.Error err = new Vista.Error("NO se ha encontradu el cine seleccionado");
            err.setVisible(true);
        }
    }//GEN-LAST:event_jBinsertarPeliActionPerformed

    private void cineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cineActionPerformed
     
    }//GEN-LAST:event_cineActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.dispose();
        ControlAcceso ca = new ControlAcceso(gtc);
        ca.setVisible(true);       
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox cine;
    private javax.swing.JButton jBinsertarPeli;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JComboBox provincias;
    // End of variables declaration//GEN-END:variables
}
